package com.dgut.crm.system.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.crm.system.entity.Department;

public class DepartmentQueryDTO{
	private String name;
	private String nature; //性质
	private String remark; //备注 

	public String getName() {
		return name;
	}

	public String getNature() {
		return nature;
	}

	public String getRemark() {
		return remark;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNature(String nature) {
		this.nature = nature;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public static Specification<Department> getSpecification(DepartmentQueryDTO departmentQueryDTO)
	{
		Specification<Department> spec = new Specification<Department>() {
			public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getName())) {
					 Predicate  p1 =  cb.like(root.get("name").as(String.class),"%"+ departmentQueryDTO.getName() + "%");
					 list.add(p1);
			      }
				 
				 if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getNature())) {
					 Predicate  p2 =  cb.like(root.get("nature").as(String.class),"%"+ departmentQueryDTO.getNature() + "%");
					 list.add(p2);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}

}
